/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Manager_Image_Utils;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.filechooser.FileNameExtensionFilter;
import manage_image.Abum;

/**
 *
 * @author Admin
 */
public class PopupMenuAbum extends JPopupMenu{

    private File f;
    Constants c = new Constants();
    private JFileChooser chooser = new JFileChooser();
    public boolean isreload=false;
    public PopupMenuAbum(File f) {
        isreload=false;
        this.f = f;
        setName("Options");
        JMenuItem copy = new JMenuItem("Import", c.icimport );
        copy.setName("Copy");
        copy.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FileNameExtensionFilter filter = new FileNameExtensionFilter(" (*.jpg)", "jpg");
                chooser.setFileFilter(filter);
                FileNameExtensionFilter filter1 = new FileNameExtensionFilter(" (*.gif)", "gif");
                chooser.setFileFilter(filter1);
                FileNameExtensionFilter filter2 = new FileNameExtensionFilter(" (*.png)", "png");
                chooser.setFileFilter(filter2);
                FileNameExtensionFilter filter3 = new FileNameExtensionFilter(" (*.tiff)", "tiff");
                chooser.setFileFilter(filter3);
                chooser.setMultiSelectionEnabled(true);
                int returnVal = chooser.showSaveDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File []files =chooser.getSelectedFiles();//
                    String path=getF().getPath();
                    for (int i=0;i<files.length;i++){
                        File f=files[i];//new File(files[i].getPath() + getF().getAbsoluteFile().getName());
                        try {
                            Library.CopyFile(f,new File(path+"//"+f.getName()));
                            Constants.flagLoadAbums=true;
                            isreload=true;
                        } catch (IOException ex) {
                            Logger.getLogger(PopupMenu.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        });
        this.add(copy);
        JMenuItem cut = new JMenuItem("Move", c.icmove);
        cut.setName("Move");
        cut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FileNameExtensionFilter filter = new FileNameExtensionFilter(" (*.jpg)", ".jpg");
                chooser.setFileFilter(filter);
                FileNameExtensionFilter filter1 = new FileNameExtensionFilter(" (*.gif)", ".gif");
                chooser.setFileFilter(filter1);
                FileNameExtensionFilter filter2 = new FileNameExtensionFilter(" (*.png)", ".png");
                chooser.setFileFilter(filter2);
                FileNameExtensionFilter filter3 = new FileNameExtensionFilter(" (*.tiff)", ".tiff");
                chooser.setFileFilter(filter3);
                chooser.setSelectedFile(Abum.GetAbumDir());
                int returnVal = chooser.showSaveDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = chooser.getSelectedFile();
                    for(File c:getF().listFiles()){
                        Library.CutFile(c, new File(getF().getPath() + c.getAbsoluteFile().getName()));
                    }
                    getF().delete();
                    isreload=true;
                    Constants.flagLoadAbums=true;
                }
            }
        });
        this.add(cut);
        JMenuItem print = new JMenuItem("Delete",c.icdelete);
        print.setName("Delete");
        print.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for(File f:getF().listFiles()){
                    f.delete();
                }
                getF().delete();
                isreload=true;
                Constants.flagLoadAbums=true;
            }
        });
        this.add(print);
    }

    public File getF() {
        return f;
    }

    public void setF(File f) {
        this.f = f;
    }
    public  void Import()
    {
        FileNameExtensionFilter filter = new FileNameExtensionFilter(" (*.jpg)", "jpg");
                chooser.setFileFilter(filter);
                FileNameExtensionFilter filter1 = new FileNameExtensionFilter(" (*.gif)", "gif");
                chooser.setFileFilter(filter1);
                FileNameExtensionFilter filter2 = new FileNameExtensionFilter(" (*.png)", "png");
                chooser.setFileFilter(filter2);
                FileNameExtensionFilter filter3 = new FileNameExtensionFilter(" (*.tiff)", "tiff");
                chooser.setFileFilter(filter3);
                chooser.setMultiSelectionEnabled(true);
                int returnVal = chooser.showSaveDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File []files =chooser.getSelectedFiles();//
                    String path=getF().getPath();
                    for (int i=0;i<files.length;i++){
                        File _f=files[i];//new File(files[i].getPath() + getF().getAbsoluteFile().getName());
                        try {
                            Library.CopyFile(_f,new File(path+"//"+_f.getName()));
                        } catch (IOException ex) {
                            Logger.getLogger(PopupMenu.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
    }
}
